package com.wjr.braveheart.ui.mvp.example;

import com.wjr.braveheart.ui.mvp.BasePresenter;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LoginPresenter extends BasePresenter<LoginModel, LoginContract.View> implements LoginContract.Presenter {
    @Override
    public void login() {
        if (isViewAttached()) {
            getView().showLoading();
            getModule().login(getView().getUserInfo(), new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    getView().onError(404, e.getMessage());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    //是否请求到所需数据
                    if ("code".equals("404")) {
                        getView().onError(404, "");
                        return;
                    }
                    if ("code".equals("200") && response.body().string() != null) {
                        getView().loginSuccess(new User());
                    } else {
                        getView().onEmpty("空数据");
                    }

                }
            });
            //获取信息之后关闭加载框
            getView().dismissLoading();
        }
    }
}
